package cn.gdcp.demo11;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btnInsertStu;
    private Button btnQueryStu;
    private Button btnDelStu;
    private Button btnUpdateStu;
    private Button btnInsertScore;
    private Button btnQueryScore;
    private Button btnDelScore;
    private Button btnUpdateScore;
    private TextView txtResult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtResult = findViewById(R.id.txt_result);

        btnInsertStu = findViewById(R.id.btn_insert_stu);
        btnInsertStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                ContentValues values = new ContentValues();
                values.put("stuno", "081101");
                values.put("name", "Lisa");
                values.put("age", 28);
                getContentResolver().insert(uri, values);
            }
        });


        btnQueryStu = findViewById(R.id.btn_query_stu);
        btnQueryStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null, null);
                if (cursor != null && cursor.getCount() > 0) {
                    StringBuffer buffer = new StringBuffer();
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        String name = cursor.getString(1);
                        int age = cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(name).append("  ").append(age).append("\n");
                    }
                    txtResult.setText(buffer);
                }else {
                    txtResult.setText("行数为0");
                }
            }
        });

        btnDelStu = findViewById(R.id.btn_del_stu);
        btnDelStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                String where = "name='Lisa'";
                int row = getContentResolver().delete(uri, where, null);
                Toast.makeText(MainActivity.this, "删除了" + row + "行", Toast.LENGTH_SHORT).show();
            }
        });

        btnUpdateStu = findViewById(R.id.btn_update_stu);
        btnUpdateStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                ContentValues values = new ContentValues();
                values.put("name", "zhangsan");
                String where = "stuno='081101'";
                int row = getContentResolver().update(uri,values,where,null);
                Toast.makeText(MainActivity.this, "更新了" + row + "行", Toast.LENGTH_SHORT).show();
            }
        });


        btnInsertScore = findViewById(R.id.btn_insert_score);
        btnInsertScore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                ContentValues values = new ContentValues();
                values.put("stuno", "081102");
                values.put("android", 68);
                values.put("english", 89);
                getContentResolver().insert(uri, values);
            }
        });

        btnQueryScore = findViewById(R.id.btn_query_score);
        btnQueryScore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null, null);
                if (cursor != null && cursor.getCount() > 0) {
                    StringBuffer buffer = new StringBuffer();
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        int english = cursor.getInt(1);
                        int android = cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(english).append("  ").append(android).append("\n");
                    }
                    txtResult.setText(buffer);
                }else {
                    txtResult.setText("行数为0");
                }
            }
        });

        btnDelScore = findViewById(R.id.btn_del_score);
        btnDelScore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                String where = "stuno='081102'";
                int row = getContentResolver().delete(uri, where, null);
                Toast.makeText(MainActivity.this, "删除了" + row + "行", Toast.LENGTH_SHORT).show();
            }
        });

        btnUpdateScore = findViewById(R.id.btn_update_score);
        btnUpdateScore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                ContentValues values = new ContentValues();
                values.put("english", 99);
                String where = "stuno='081102'";
                int row = getContentResolver().update(uri,values,where,null);
                Toast.makeText(MainActivity.this, "更新了" + row + "行", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
